import collections


class SnakeGame(object):

    def __init__(self, width, height, food):
        """
        :type width: int
        :type height: int
        :type food: List[List[int]]
        """
        self.width = width
        self.height = height
        self.food = food
        self.food_index = 0
        self.body = collections.deque()
        self.head = (0, 0)
        self.body.append(self.head)
        self.body_set = {self.head}
        self.score = 0

    def move(self, direction):
        """
        :type direction: str
        :rtype: int
        """
        if direction == 'U':
            self.head = (self.head[0] - 1, self.head[1])
        if direction == 'D':
            self.head = (self.head[0] + 1, self.head[1])
        if direction == 'L':
            self.head = (self.head[0], self.head[1] - 1)
        if direction == 'R':
            self.head = (self.head[0], self.head[1] + 1)
        if self.head[0] < 0 or self.head[0] == self.height or self.head[1] < 0 or self.head[1] == self.width:
            return - 1
        else:
            if self.food_index < len(self.food) and self.head[0] == self.food[self.food_index][0] and self.head[1] == self.food[self.food_index][1]:
                self.score += 1
                self.body.append(self.head)
                self.body_set.add(self.head)
                self.food_index += 1
                return self.score
            else:
                node = self.body.popleft()
                self.body_set.remove(node)
                if self.head in self.body_set:
                    return -1
                else:
                    self.body.append(self.head)
                    self.body_set.add(self.head)
                    return self.score
